//
//  RegisterViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/4/11.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (strong, nonatomic) IBOutlet UITextField *registerPhoneField;
@property (strong, nonatomic) IBOutlet UITextField *registCodeField;
@property (strong, nonatomic) IBOutlet UITextField *passwordField;
@property (strong, nonatomic) IBOutlet UIButton *getNumberButton;
@property (strong, nonatomic) IBOutlet UIButton *commitButton;
@property(nonatomic,strong)NSTimer * timer;
@property(nonatomic,assign)NSInteger  time;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initViewLayout];
}


- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    _scroView.contentSize = flexibleSize(CGSizeMake(320, 505), NO);
    
    self.commitButton.layer.cornerRadius = 5*VerticalRatio();
    
    [_registerPhoneField setValue:[UIColor colorWithWhite:0.588 alpha:1.000]forKeyPath:@"_placeholderLabel.textColor"];
    
    [_registCodeField setValue:[UIColor colorWithWhite:0.588 alpha:1.000]
                    forKeyPath:@"_placeholderLabel.textColor"];
    [_passwordField setValue:[UIColor colorWithWhite:0.588 alpha:1.000]
                  forKeyPath:@"_placeholderLabel.textColor"];
    
}


- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma  mark  == 获取验证码
- (IBAction)getRegistNumber:(id)sender {
    if ([Common dataIsNULL:_registerPhoneField.text]) {
        [CWHUDTips showTips:_registerPhoneField.placeholder];
        return;
    }
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_registerPhoneField.text forKey:@"phone"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:JY_registerCode withHttpType:@"POST" withTag:JY_JY_registerCode];
    [self hideKeyBoard];
}

#pragma mark  == 注册
- (IBAction)sureRegist:(id)sender {
    if ([Common dataIsNULL:_registCodeField.text]) {
        [CWHUDTips showTips:_registCodeField.placeholder];
        return;
    }
    if ([Common dataIsNULL:_passwordField.text]) {
        [CWHUDTips showTips:_passwordField.placeholder];
        return;
    }
    if ([Common dataIsNULL:_passwordField.text]) {
        [CWHUDTips showTips:_passwordField.placeholder];
        return;
    }
    

    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_registerPhoneField.text forKey:@"userName"];
    [dic setObject:_registCodeField.text forKey:@"code"];
    [dic setObject:_passwordField.text forKey:@"password"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:JY_appUserRegister withHttpType:@"POST" withTag:JY_JY_appUserRegister];
    [self hideKeyBoard];
}



#pragma mark -- Timer

- (void)changeTime{
    
    _time ++;
    NSString * time = [NSString stringWithFormat:@"%d秒后重发",60-_time];
    [_getNumberButton setTitle:time forState:UIControlStateNormal];
    [_getNumberButton setTitleColor:[UIColor colorWithWhite:0.588 alpha:1.000] forState:UIControlStateNormal];
    _getNumberButton.enabled = NO;
    if (_time == 60) {
        [self stopTimer];
        [_getNumberButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_getNumberButton setTitleColor:[UIColor colorWithRed:0.667 green:0.867 blue:0.843 alpha:1.000] forState:UIControlStateNormal];
        _getNumberButton.enabled = YES;
    }
}

- (void)startTimer{
    _time = 0;
    [_timer fire];
}

- (void)stopTimer{
    [_timer invalidate];
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    if (tag==JY_JY_registerCode) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
            _timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
            [self startTimer];
            
        }
        
    }else if(tag == JY_JY_appUserRegister){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [self dismissViewControllerAnimated:YES completion:^{
                [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            }];
        }
        
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
